package com.db4o.odbgen.plugins.java.maingenerator.typedxmlschema;

import java.util.Iterator;
import java.util.Map;

import com.db4o.odbgen.OdbgenException;


/**
 * A collection of {@link TypeDeclaration} objects.
 * @author liviug
 */
public class TypeDeclarationCollection implements Iterable<TypeDeclaration> {
	private Map<String, TypeDeclaration> _typeDeclarations = new java.util.Hashtable<String, TypeDeclaration>();

	/**
	 * Adds a new type to the collection.
	 * Does not allow duplicates.
	 */
	public void add(TypeDeclaration value) throws OdbgenException {
		if(this.contains(value.getQualifiedName())){
			throw new OdbgenException("Type '%s' already exists.", value.getQualifiedName());
		}
		this._typeDeclarations.put(value.getQualifiedName(), value);
	}

	public void clear() {
		this._typeDeclarations.clear();
	}

	public boolean contains(String qualifiedName) {
		return this._typeDeclarations.containsKey(qualifiedName);
	}

	public boolean isEmpty() {
		return this._typeDeclarations.isEmpty();
	}

	public int size() {
		return this._typeDeclarations.size();
	}

	@Override
	public Iterator<TypeDeclaration> iterator() {
		return  this._typeDeclarations.values().iterator();
	}

	/**
	 * Returns a type based on it's qualified name.
	 * See {@link Map}.get() for details.
	 */
	public TypeDeclaration get(String qualifiedName) {
		return this._typeDeclarations.get(qualifiedName);
	}
}
